// Admin Controller

function categoryCtrl($scope, $http, $route, $rootScope, $timeout, commonService, $location) {
    $scope.categories = {};
	_log('### categoryCtrl ###');

	    var renderTable = function(){
		
		var url = 'category_ws.php'; // TODO
		var params = {
		        wsname: 'getallcategories', // TODO
		        token: _pageData.tokenKey
		};
		var rootData = 'categories'; // TODO
		
		 _log('### renderTable ###');
		 
		var t = $('table').dataTable( {
	            "ajax":{ 
	                    url: _COMMON.context + url+"?wsname="+params.wsname+"&token="+params.token,
	                    dataSrc: rootData
	                },
	            "columns": [
	                { "data": "id",  "sClass" : "center"},
	                { "data": "name" },
	                { "data": "title" },
	                { "data": "description" }
	            ],
	            "columnDefs": [ 
	                   {
	                       "render": function ( data, type, row ) {
	                	   return '<button title="Edit" type="button" class="btn btn-table-action btn-warning" onclick="_pageFn.onCategoryEdit('+data+')"><i class="fa fa-nontext fa-edit"></i></button>&nbsp;'+
	                   	'<button title="Delete"type="button" class="btn btn-table-action btn-danger" onclick="_pageFn.onCategoryDelete('+data+')"><i class="fa fa-nontext fa-trash-o"></i></button>';
	                       },
	                       "targets": 0
	                   }
	               ],
//			"sScrollY": "300px",
			"sScrollX": "100px",
			"sScrollXInner": "100%",
			"bScrollCollapse": true,
			"bPaginate": true,
			"sPaginationType": "full_numbers"
	                      
	        } );
		
//		new $.fn.dataTable.FixedColumns( t, {
//	        	"iLeftColumns": 1,
//	        	"iRightColumns":0
//	        } );
		
	    };

    _pageFn.onCategoryEdit = function(id) {
	location = '#/admin/category/category?catid='+id;
    };

    _pageFn.onCategoryDelete = function (id) {

        $("#confirm").html("Do you want to delete ?");

        $("#confirm").dialog({
            resizable: false,
            modal: true,
            title: "Confirm Delete",
            height: 180,
            width: 300,
            buttons: {
                "Yes": function () {
                    $(this).dialog('close');
                    callback(id);
                },
                    "No": function () {
                    $(this).dialog('close');
                }
            }
        });
    };

    function callback(value) {
        if (value) {
      
             var config = {
	            params : {
		        wsname : 'deletecategory',
		        token : _pageData.tokenKey,
		        id : value
	            }
	        };
	
	        $http.post(_COMMON.context + url, null, config)
	          .success(function(obj) {
	              //alert('Delete Success !');
                    notif({
                      msg: "<b><i class='fa fa-thumbs-o-up'></i>  Delete Success !</b>",
                      type: "success",
                      position: "center"
                    });

                    location= '#/admin/category/list/'; //refresh.

                  })
                  .error(function() {
                      alert('Error is occured, May be database connect is wrong');
                  });
       

        } else {
            alert("Rejected");
        }

    }
    
    
    // Start Render table 
    renderTable();


}
